# $Id: filetypes.tcl 36 2007-08-29 17:52:22Z khomoutov $
# Mapping of known file extensions and names to their types.

namespace export filetype

if 0 {
    * 1 for any file type
    * 2 for audio files ("mp3", "mp2", "wav", "au", "rm", "mid", "sm")
    * 3 for compressed files ("zip", "arj", "rar", "lzh", "gz", "z", "arc", "pak")
    * 4 for documents ("doc", "txt", "wri", "pdf", "ps", "tex")
    * 5 for executables ("pm", "exe", "bat", "com")
    * 6 for pictures ("gif", "jpg", "jpeg", "bmp", "pcx", "png", "wmf", "psd")
    * 7 for video ("mpg", "mpeg", "avi", "asf", "mov")
    * 8 for folders
}
variable ext2type
variable name2type

# 1 -- UNKNOWN:
array set ext2type {
	drc           1
	dss           1
	mpcpl         1
	rp            1
	rt            1
	smi           1
	smil          1
}

# 2 -- audio:
array set ext2type {
	669     2
	8svx    2
	aif     2
	aifc    2
	aiff    2
	aac     2
	ac3     2
	amf     2
	amr     2
	ams     2
	ape     2
	apl     2
	asf     2
	asx     2
	avi     2
	au      2
	cda     2
	cue     2
	dts     2
	dsm     2
	far     2
	fla     2
	flac    2
	fpl     2
	iff     2
	it      2
	itz     2
	m1a     2
	m2a     2
	m3u     2
	m3u8    2
	m4a     2
	m4b     2
	m4p     2
	mac     2
	mdl     2
	mdr     2
	mdz     2
	med     2
	mid     2
	midi    2
	mka     2
	mod     2
	mol     2
	mp1     2
	mp2     2
	mp3     2
	mpa     2
	mp+     2
	mpc     2
	mpp     2
	mtm     2
	nst     2
	ogg     2
	okt     2
	pls     2
	ptm     2
	ra      2
	rmi     2
	s3m     2
	s3z     2
	snd     2
	spx     2
	stm     2
	svx     2
	ult     2
	umx     2
	wav     2
	wax     2
	wma     2
	wmx     2
	wow     2
	wv      2
	xm      2
	xmz     2
}

# 3 -- compressed:
array set ext2type {
	7z      3
	ace     3
	arc     3
	arj     3
	bz2     3
	gz      3
	lha     3
	lzh     3
	pak     3
	rar     3
	z       3
	zip     3
}

# 4 -- documents:
array set ext2type {
	chm     4
	doc     4
	eps     4
	hlp     4
	htm     4
	html    4
	info    4
	latex   4
	ltx     4
	mht     4
	pdf     4
	ppt     4
	ps      4
	shtml   4
	tex     4
	texinfo 4
	txt     4
	wri     4
	xhtml   4
	xls     4
	xml     4
}

# 5 -- executables:
array set ext2type {
	cmd     5
	com     5
	bat     5
	exe     5
	vbs     5
}

# 6 -- pictures:
array set ext2type {
	ai      6
	cdr     6
	bmp     6
	gif     6
	ico     6
	jng     6
	jpg     6
	jpeg    6
	mng     6
	pcx     6
	pgm     6
	pixmap  6
	png     6
	ppm     6
	psd     6
	raw     6
	sgi     6
	sun     6
	svg     6
	svgz    6
	tga     6
	tiff    6
	wmf     6
	xbm     6
	xpm     6
}

# 7 -- video:
# TODO "dsm", "mp4" also qualify as audio
array set ext2type {
	3g2     7
	3gp2    7
	3gp     7
	3gpp    7
	asf     7
	avi     7
	bik     7
	d2v     7
	divx    7
	dsm     7
	flc     7
	fli     7
	flic    7
	flv     7
	ifo     7
	ivf     7
	m1v     7
	m2v     7
	m4v     7
	mkv     7
	mov     7
	mp2v    7
	mp4     7
	mpe     7
	mpeg    7
	mpg     7
	mpv2    7
	ogm     7
	pva     7
	qt      7
	roq     7
	ratdvd  7
	ram     7
	rm      7
	rmvb    7
	smk     7
	sub     7
	swf     7
	tp      7
	ts      7
	vob     7
	vp3     7
	vp4     7
	vp5     7
	vp6     7
	wmv     7
	wvx     7
}

# 4 -- documents:
array set name2type {
	makefile    4
	readme      4
	todo        4
	changelog   4
	thanks      4
	authors     4
	install     4
	howto       4
}

proc filetype fname {
	set ext [string trimleft [file extension $fname] .]

	set type 1 ;# "any" == unknown

	hook run share_type_of_file #[info level] fname ext type

	set type
}

proc GuessFileType {level vname vext vtype} {
	variable ext2type
	variable name2type

	upvar $level $vname name $vext ext $vtype type

	if {$ext ne ""} {
		if {[info exists ext2type($ext)]} {
			set type $ext2type($ext)
			return
		}
	} else {
		if {[info exists name2type($name)]} {
			set type $name2type($name)
			return
		}
	}
}

hook add share_type_of_file [mycmd GuessFileType] -iscommand -prio 100

